package com.lipiao.nb_iot;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import com.github.glomadrian.velocimeterlibrary.VelocimeterView;
import com.robinhood.ticker.TickerUtils;
import com.robinhood.ticker.TickerView;

import java.util.Timer;
import java.util.TimerTask;

import static com.lipiao.nb_iot.MainActivity.viewPager;

public class StatusFragment extends Fragment {
    View rootView;

    static double temperature = 9.9;

    //三种浓度view
    private SeekBar seek;
    private VelocimeterView velocimeter;
    private VelocimeterView velocimeter2;
    private VelocimeterView velocimeter3;

    //三个数值的ticView
    private TickerView ticTem;//温度
    private TickerView ticDam;//湿度
    private TickerView ticSmo;//烟雾


    static double getTemperature() {
        return temperature;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_status, container, false);
        initView();
        initListener();
        return rootView;
    }

    private void initView() {
        seek = rootView.findViewById(R.id.seek);
        seek.setOnSeekBarChangeListener(new StatusFragment.SeekListener());
        velocimeter = rootView.findViewById(R.id.velocimeter);//温度
        velocimeter2 = rootView.findViewById(R.id.velocimeter2);//湿度
        velocimeter3 = rootView.findViewById(R.id.velocimeter3);//烟雾浓度

        //tickerview初始化
        ticTem = rootView.findViewById(R.id.tic_temperature);
        ticTem.setCharacterLists(TickerUtils.provideNumberList());
        ticDam = rootView.findViewById(R.id.tic_dampness);
        ticDam.setCharacterLists(TickerUtils.provideNumberList());
        ticSmo = rootView.findViewById(R.id.tic_smoke_scope);
        ticSmo.setCharacterLists(TickerUtils.provideNumberList());

    }

    private void initListener() {

    }

    private class SeekListener implements SeekBar.OnSeekBarChangeListener {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            velocimeter.setValue(progress, true);
            velocimeter2.setValue(progress / 2, true);
            velocimeter3.setValue(progress / 3, true);

            //同步至TicView
            ticTem.setText("" + progress);
            ticDam.setText("" + progress / 2);
            ticSmo.setText("" + progress / 3);

            //同步至类变量，用于传值
            temperature = progress;
            if(progress>=80){
                viewPager.setCurrentItem(1);
                final Intent it_1 = new Intent(getActivity(), Person_Activity.class);
                Timer timer_1 = new Timer();
                TimerTask task_1 = new TimerTask() {
                    @Override
                    public void run() {
                        startActivity(it_1);
                        //getActivity().finish();
                    }
                };
                timer_1.schedule(task_1, 0);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            //Empty
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            //Empty
        }
    }
}
